package com.sf.fy.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.request.operate.SysPromotionStatisticsRequest;
import com.sf.fy.uav.common.response.operational.SysActivityResponse;
import com.sf.fy.uav.common.response.operational.SysPromotionStatisticsResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.vo.TotalAmountVo;
import com.sf.fy.uav.entity.PromotionRecords;
import com.sf.fy.uav.entity.PromotionStatistics;
import com.sf.fy.uav.mapper.PromotionRecordsMapper;
import com.sf.fy.uav.mapper.PromotionStatisticsMapper;
import com.sf.fy.uav.service.IPromotionStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
 * <p>
 * 推广记录表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-08-18
 */
@Service
public class PromotionStatisticsServiceImpl extends ServiceImpl<PromotionStatisticsMapper, PromotionStatistics> implements IPromotionStatisticsService {

    @Autowired
    private PromotionRecordsMapper promotionRecordsMapper;

    @Autowired
    private PromotionStatisticsMapper promotionStatisticsMapper;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;

    @Override
    public boolean save(PromotionStatistics entity) {
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(Collection<PromotionStatistics> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<PromotionStatistics> entityList) {
        return super.saveOrUpdateBatch(entityList);
    }

    @Override
    public boolean removeById(PromotionStatistics entity) {
        return super.removeById(entity);
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        return super.removeByMap(columnMap);
    }

    @Override
    public boolean remove(Wrapper<PromotionStatistics> queryWrapper) {
        return super.remove(queryWrapper);
    }

    @Override
    public boolean removeByIds(Collection<?> list, boolean useFill) {
        return super.removeByIds(list, useFill);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list, boolean useFill) {
        return super.removeBatchByIds(list, useFill);
    }

    @Override
    public boolean updateById(PromotionStatistics entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean update(Wrapper<PromotionStatistics> updateWrapper) {
        return super.update(updateWrapper);
    }

    @Override
    public boolean update(PromotionStatistics entity, Wrapper<PromotionStatistics> updateWrapper) {
        return super.update(entity, updateWrapper);
    }

    @Override
    public boolean updateBatchById(Collection<PromotionStatistics> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    public PromotionStatistics getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public List<PromotionStatistics> listByIds(Collection<? extends Serializable> idList) {
        return super.listByIds(idList);
    }

    @Override
    public List<PromotionStatistics> listByMap(Map<String, Object> columnMap) {
        return super.listByMap(columnMap);
    }

    @Override
    public PromotionStatistics getOne(Wrapper<PromotionStatistics> queryWrapper) {
        return super.getOne(queryWrapper);
    }

    @Override
    public long count() {
        return super.count();
    }

    @Override
    public long count(Wrapper<PromotionStatistics> queryWrapper) {
        return super.count(queryWrapper);
    }

    @Override
    public List<PromotionStatistics> list(Wrapper<PromotionStatistics> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Override
    public List<PromotionStatistics> list() {
        return super.list();
    }

    @Override
    public <E extends IPage<PromotionStatistics>> E page(E page, Wrapper<PromotionStatistics> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public <E extends IPage<PromotionStatistics>> E page(E page) {
        return super.page(page);
    }

    @Override
    public List<Map<String, Object>> listMaps(Wrapper<PromotionStatistics> queryWrapper) {
        return super.listMaps(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> listMaps() {
        return super.listMaps();
    }

    @Override
    public List<Object> listObjs() {
        return super.listObjs();
    }

    @Override
    public <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return super.listObjs(mapper);
    }

    @Override
    public List<Object> listObjs(Wrapper<PromotionStatistics> queryWrapper) {
        return super.listObjs(queryWrapper);
    }

    @Override
    public <V> List<V> listObjs(Wrapper<PromotionStatistics> queryWrapper, Function<? super Object, V> mapper) {
        return super.listObjs(queryWrapper, mapper);
    }

    @Override
    public <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<PromotionStatistics> queryWrapper) {
        return super.pageMaps(page, queryWrapper);
    }

    @Override
    public <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return super.pageMaps(page);
    }

    @Override
    public QueryChainWrapper<PromotionStatistics> query() {
        return super.query();
    }

    @Override
    public LambdaQueryChainWrapper<PromotionStatistics> lambdaQuery() {
        return super.lambdaQuery();
    }

    @Override
    public LambdaQueryChainWrapper<PromotionStatistics> lambdaQuery(PromotionStatistics entity) {
        return super.lambdaQuery(entity);
    }


    @Override
    public UpdateChainWrapper<PromotionStatistics> update() {
        return super.update();
    }

    @Override
    public LambdaUpdateChainWrapper<PromotionStatistics> lambdaUpdate() {
        return super.lambdaUpdate();
    }

    @Override
    public boolean saveOrUpdate(PromotionStatistics entity, Wrapper<PromotionStatistics> updateWrapper) {
        return super.saveOrUpdate(entity, updateWrapper);
    }

    @Override
    public Long getTotal(String phoneNumber, String activityId) {
        QueryWrapper<PromotionStatistics> promotionStatisticsQueryWrapper = this.getQueryWrapper(phoneNumber);
        promotionStatisticsQueryWrapper.eq("activity_id", activityId);
        return promotionStatisticsMapper.selectCount(promotionStatisticsQueryWrapper);
    }

    @Override
    public TotalAmountVo getPromotionStatistics(SysPromotionStatisticsRequest sysPromotionStatisticsRequest) {
        Integer page = sysPromotionStatisticsRequest.getPage();
        Integer size = sysPromotionStatisticsRequest.getSize();
        Page<PromotionStatistics> promotionStatisticsPage = new Page<>(page, size);
        String phoneNumber = sysPromotionStatisticsRequest.getPhoneNumber();
        QueryWrapper<PromotionStatistics> promotionStatisticsQueryWrapper = this.getQueryWrapper(phoneNumber);
        promotionStatisticsQueryWrapper.eq("activity_id", sysPromotionStatisticsRequest.getActivityId());
        promotionStatisticsQueryWrapper.orderByDesc("create_time");
        IPage<PromotionStatistics> promotionStatisticsIPage = promotionStatisticsMapper.selectPage(promotionStatisticsPage, promotionStatisticsQueryWrapper);
        List<PromotionStatistics> promotionStatisticsList = promotionStatisticsMapper.selectList(promotionStatisticsQueryWrapper);
        BigDecimal totalRewardSum =promotionStatisticsList.stream().map(PromotionStatistics::getTotalReward).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
        List<SysPromotionStatisticsResponse> sysPromotionStatisticsResponses = BeanUtil.copyProperties(promotionStatisticsIPage.getRecords(), SysPromotionStatisticsResponse.class);
        for (SysPromotionStatisticsResponse sysPromotionStatisticsRespons : sysPromotionStatisticsResponses) {
            if (sysPromotionStatisticsRespons.getTotalReward() == null) {
                sysPromotionStatisticsRespons.setTotalReward(BigDecimal.ZERO);
            }
            String pPhoneNumber = phoneEncoderUtil.decryptString(sysPromotionStatisticsRespons.getPhoneNumber());
            sysPromotionStatisticsRespons.setPhoneNumber(pPhoneNumber.substring(0, 3) + "****" + pPhoneNumber.substring(7));
        }
        TotalAmountVo totalAmountVo = new TotalAmountVo();
        totalAmountVo.setTotalRewardAdd(totalRewardSum.setScale(2, RoundingMode.HALF_UP));
        totalAmountVo.setSysPromotionStatisticsResponseList(sysPromotionStatisticsResponses);
        return totalAmountVo;
    }

    @Override
    public PromotionStatistics getPromotionStatistics(String userId, String activityId) {
        //处理统计数据
        QueryWrapper<PromotionStatistics> promotionStatisticsQueryWrapper = new QueryWrapper<>();
        promotionStatisticsQueryWrapper.eq("user_id", userId);
        promotionStatisticsQueryWrapper.eq("activity_id", activityId);
        PromotionStatistics promotionStatistics = promotionStatisticsMapper.selectOne(promotionStatisticsQueryWrapper);
        return promotionStatistics;
    }


    private QueryWrapper getQueryWrapper(String phoneNumber) {
        QueryWrapper<PromotionStatistics> promotionStatisticsQueryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(phoneNumber)) {
            promotionStatisticsQueryWrapper.eq("phone_number", phoneEncoderUtil.encryptString(phoneNumber));
        }
        return promotionStatisticsQueryWrapper;
    }
}
